package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"mall_admin_api/biz/model"
)

func (c *AdminRoleMenuRelationDAO) GetPermissionByAdminID(ctx context.Context, adminID int64) ([]string, error) {
	list, err := AdminMenu.WithContext(ctx).
		LeftJoin(AdminRoleMenuRelation, AdminRoleMenuRelation.AdminMenuID.EqCol(AdminMenu.ID)).
		LeftJoin(AdminRole, AdminRole.ID.EqCol(AdminRoleMenuRelation.AdminRoleID)).
		LeftJoin(AdminRoleRelation, AdminRole.ID.EqCol(AdminRoleRelation.AdminRoleID)).
		Where(AdminRoleRelation.AdminID.Eq(adminID)).
		Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "find admin role relation error: %v", err)
		return nil, err
	}
	var permissions []string
	for _, menu := range list {
		permissions = append(permissions, menu.Permission)
	}
	return permissions, nil
}

func (c *AdminRoleMenuRelationDAO) GetMenuListByAdminID(ctx context.Context, adminID int64) ([]*model.AdminMenu, error) {
	list, err := AdminMenu.WithContext(ctx).
		LeftJoin(AdminRoleMenuRelation, AdminRoleMenuRelation.AdminMenuID.EqCol(AdminMenu.ID)).
		LeftJoin(AdminRole, AdminRole.ID.EqCol(AdminRoleMenuRelation.AdminRoleID)).
		LeftJoin(AdminRoleRelation, AdminRole.ID.EqCol(AdminRoleRelation.AdminRoleID)).
		Where(AdminRoleRelation.AdminID.Eq(adminID)).
		Order(AdminMenu.Sort).
		Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "find admin role relation error: %v", err)
		return nil, err
	}

	return list, nil
}
